package com.qc31.monitorlib.color

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.PopupWindow
import androidx.recyclerview.widget.GridLayoutManager
import com.qc31.monitorlib.databinding.PopupColorsBinding

/** @Author Jasper @Time 2021/11/30 10:15 @Desc */
class ColorsPopup(private val context: Context, private val titleId: Int, val callback: Callback) :
    PopupWindow() {
    private val mColorAdapter = ColorAdapter()
    private val binding = PopupColorsBinding.inflate(LayoutInflater.from(context))

    //    private val materialColors = intArrayOf(
//        -0xffffff,
//        -0x853998,
//        -0x3400,
//        -0x5e00,
//        -0x662a02,
//        -0xffdc,
//        -0x28598b,
//        -0x274a44,
//        -0x8c437c,
//        -0x422322,
//        -0xb79532,
//        -0x1509c,
//        -0x211932,
//        -0x938c95,
//        -0xc54d2b,
//        -0x281951,
//        -0x3d3d3e,
//        -0xc86e17,
//        -0xbd2c88,
//        -0x333333
//    )
    private val materialColors = arrayListOf<String>(
        "#FFFFFF",
        "#7AC668",
        "#FFCC00",
        "#FFA200",
        "#FFA200",
        "#FF0024",
        "#D7A675",
        "#D8B5BC",
        "#73BC84",
        "#BDDCDE",
        "#486ACE",
        "#FEAF64",
        "#DEE6CE",
        "#6C736B",
        "#3AB2D5",
        "#D7E6AF",
        "#C2C2C2",
        "#3791E9",
        "#42D378",
        "#333333"
    )


    init {
        contentView = binding.root
        isFocusable = true //popupwindow设置焦点
        setBackgroundDrawable(ColorDrawable(-0x56000000)) //设置背景
        isOutsideTouchable = true //点击外面窗口消失
        width = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的宽
        height = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的高
        init()

    }


    fun init() {
        binding.tvColorTitle.setText(titleId)

        binding.recyclerColor.layoutManager = GridLayoutManager(context, 5)
        binding.recyclerColor.adapter = mColorAdapter
        mColorAdapter.setList(materialColors.toMutableList())

        mColorAdapter.setOnItemClickListener { adapter, view, position ->
            mColorAdapter.index = position
            mColorAdapter.notifyDataSetChanged()
        }

        binding.tvColorTitle.setOnClickListener {
            dismiss()
        }
        binding.tvColorSure.setOnClickListener {
            callback.callback(mColorAdapter.index, mColorAdapter.data[mColorAdapter.index])
            dismiss()
        }
    }

    fun showPopup(view: View, color: String) {
        mColorAdapter.index = getIndex(color)
        mColorAdapter.notifyDataSetChanged()
        showAtLocation(view, Gravity.NO_GRAVITY, 0, 0)
    }

    private fun getIndex(color: String): Int {
        materialColors.forEachIndexed { index, i ->
            if (i == color) return index
        }
        return 0
    }

    interface Callback {
        fun callback(index: Int, color: String)
    }

}